package com.xmut.bs.handaccount.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.ImageView;

import com.xmut.bs.handaccount.GlideApp;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.base.CommonListActivity;
import com.xmut.bs.handaccount.bmob.Article;
import com.xmut.bs.handaccount.bmob.User;
import com.xmut.bs.handaccount.util.DisplayUtils;
import com.xmut.bs.handaccount.util.DividerItemDecoration;
import com.xmut.bs.handaccount.util.GlideCircleTransform;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.Collections;
import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

import static com.xmut.bs.handaccount.base.Constants.PARAM_ARTICLE_ID;

/**
 * Description
 * Created by Dollkey on 2018/4/11.
 */

public class MyArticleActivity extends CommonListActivity<Article> {

    private ItemTouchHelper mItemTouchHelper;

    @Override
    public void afterInit() {
        mItemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {

            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                return makeMovementFlags(0, ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                if (mComList != null) {
                    // 更换数据源中的数据Item的位置
                    Collections.swap(mComList, viewHolder.getAdapterPosition(), target.getAdapterPosition());
                    // 更新UI中的Item的位置，主要是给用户看到交互效果
                    mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
                    return true;
                }
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                final Article adv = mComList.get(viewHolder.getAdapterPosition());
                adv.delete(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            showToast("删除成功");
                            mComList.remove(adv);
                            mAdapter.notifyDataSetChanged();
                        } else {
                            showToast("失败：" + e.getMessage() + "," + e.getErrorCode());
                        }
                    }
                });
            }
        });
        mItemTouchHelper.attachToRecyclerView(mRecycleView);
    }

    @Override
    protected String getSubTitle() {
        return "我的文章";
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return new DividerItemDecoration(mContext,
                DisplayUtils.dp2px(mContext, 8),
                getResources().getColor(R.color.grey100)).setHasLeftOrRight(false).setHasTopAndBottom(false);
    }

    @NonNull
    @Override
    protected RecyclerView.LayoutManager getRvLayoutManager() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    protected RecyclerView.Adapter getAdapter(List<Article> comList) {
        return new CommonAdapter<Article>(mContext, R.layout.item_article, comList) {
            @Override
            protected void convert(ViewHolder holder, final Article article, int position) {
                holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent().putExtra(PARAM_ARTICLE_ID, article), ArticleDetailActivity.class);
                    }
                });
                holder.setText(R.id.goods_name, article.getTitle());
                holder.setText(R.id.username, article.getUser().getNickName());
                ImageView coverImg = holder.getView(R.id.cover_img);
                ImageView avatarImg = holder.getView(R.id.avatar_img);
                GlideApp.with(coverImg).load(article.getCoverImg()).override(1200, 600).centerCrop().into(coverImg);
                GlideApp.with(coverImg).load(article.getUser() == null ? "" : article.getUser().getAvatar())
                        .placeholder(R.mipmap.ic_head_img).error(R.mipmap.ic_head_img)
                        .transform(new GlideCircleTransform(mContext)).override(60, 60).into(avatarImg);
            }
        };
    }

    @Override
    public void getData(int pageNo, int pageSize) {
        mBmobQuery.addWhereEqualTo("user", BmobUser.getCurrentUser(User.class));
        mBmobQuery.include("user");
        mBmobQuery.setSkip(pageNo * pageSize).setLimit(pageSize).findObjects(new FindListener<Article>() {
            @Override
            public void done(List<Article> list, BmobException e) {
                if (e == null) {
                    showList(list);
                } else {
                    showError("请求失败：" + e.getMessage());
                }
            }
        });
    }

    @Override
    protected void initParams(Intent intent) {

    }
}
